<?php
declare (strict_types = 1);

namespace app\patient\controller;
use app\Db2;
use app\AppAuthController;
use EasyWeChat\Factory;



class Classes extends AppAuthController{
    /**
     * @Notes: 获取分类/诊断列表
     * @Interface getClassesList
     * @author: Shaco
     * @Time: 2022/9/21-20:27
     */
    public function getClassesList(){
        $type = input('type'); //xy  zy

        if($type !== 'xy' && $type !== 'zy') return rejson(302, '参数错误');

        $list = Db2::get_datalist("{$type}_cengji",[[]],['id','pid','name', 'yfid', 'type']);
        $data = array_to_tree($list);

        return rejson(200, '操作成功', $data);
    }

    /**
     * @Notes: 添加分类
     * @Interface addClasses
     * @author: Shaco
     * @Time: 2022/9/21-20:08
     */
    public function saveClasses(){
        $id = input('id'); // 有id为编辑
        $name = input('name'); // 名字必填
        $fid = input('pid'); // 一级为固定导入 必填
        $type = input('type');  // 1分类 2诊断
        $tableType = input('tableType'); //zy  xy

        if(!$name || $name == 'undefined') return rejson(302,'必填字段缺失');
        if(!$id || $id == 'undefined'){
            if(!$fid || $fid == 'undefined') return rejson(302,'必填字段缺失');
            if(!$type || $type == 'undefined') return rejson(302,'必填字段缺失');
            if(!$tableType || $tableType == 'undefined') return rejson(302,'必填字段缺失');
        }

        if($id){
            Db2::update_data("{$tableType}_cengji",[['id' => $id]],['name' => $name]);
            return rejson(200, '操作成功');
        }else{
            $num = Db2::insert_data("{$tableType}_cengji", ['name' => $name,'pid' => $fid,'add_time' => time(),'type' => $type]);
            return $num > 0 ? rejson(200, '操作成功') : rejson(0,'操作失败');
        }
    }

    /**
     * @Notes: 保存诊断记录
     * @Interface saveRecords
     * @author: Shaco
     * @Time: 2022/10/3-11:05
     */
    public function saveRecords(){
        $user = $this->user;
        $uid = $user['id'];
        $tongue = input('tongue');
        $otherRecords = input('otherRecords');
        $type = input('type');
        $text1 = input('text1'); // json格式
        $cancer_remark = input('remark');
        $cityid = input('cityid');
        

        if(!$tongue || $tongue == 'undefined') return rejson(302, '必填字短缺失');

        $config = [
            // 必要配置
            'app_id'             => 'wx485998504983c8a1',
            'mch_id'             => '1646284594',
            'key'                => '7e1e85f1d56e05fd3febcafdbdf003c4',   // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
        ];

        $app = Factory::payment($config);

        $order = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

        $result = $app->order->unify([
            'body' => '医康宝',
            'out_trade_no' => $order,
            'total_fee' => 1,
            'notify_url' => 'https://ykbwxapp.icanbo.com.cn/index.php/patient/pay/notify_url',     // 你也可以在下单时单独设置来想覆盖它
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $user['wx_app_openid'],
        ]);

        if (isset($result['err_code'])) {
            return rejson(0,'参数错误，请重新下单', $data);
        } else {
            $jssdk = $app->jssdk;
            $config = $jssdk->sdkConfig($result['prepay_id']);
            $config['timeStamp'] = $config['timestamp'];//此处需要将小写s变为大写

            $data = [
                'order_num' => $order,
                'userid' => $uid,
                'tongue' => $tongue,
                'add_time' => time(),
                'text1' => $text1,
                'cityid' => $cityid
            ];

            print_r($data);die;

            if($otherRecords) $data['other_picture'] = $otherRecords;
            if($type) $data['type'] = $type;
            if($cancer_remark) $data['cancer_remark'] = $cancer_remark;

            $num = Db2::insert_data('zhenduanjilu',$data);

            if ($num) {
                return rejson(200,'操作成功', $query);
            } else {
                return rejson(0,'操作失败', $query);
            }
        }
    }
    
    public function requestPayment_query()
    {
        $user = check_login(Request::header('Authorization'));
        
        $config = [
            // 必要配置
            'app_id'             => 'wx485998504983c8a1',
            'mch_id'             => '1646284594',
            'key'                => '7e1e85f1d56e05fd3febcafdbdf003c4',   // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
        ];

        $app = Factory::payment($config);

        $order = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

        $result = $app->order->unify([
            'body' => '医康宝',
            'out_trade_no' => '20150806125357',
            'total_fee' => 1,
            'notify_url' => 'https://ykbwxapp.icanbo.com.cn/index.php/patient/pay/notify_url',     // 你也可以在下单时单独设置来想覆盖它
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $user['wx_app_openid'],
        ]);

        if (isset($result['err_code'])) {
            return rejson(0,'参数错误，请重新下单', $data);
        } else {
            $jssdk = $app->jssdk;
            $config = $jssdk->sdkConfig($result['prepay_id']);
            $config['timeStamp'] = $config['timestamp'];//此处需要将小写s变为大写

            return rejson(200,'操作成功', $config);
        }
    }

    /**
     * @Notes: 根据四级分类id获取药方
     * @Interface getYaofang
     * @author: Shaco
     * @Time: 2022/10/10-10:25
     */
    public function getYaofang(){
        $id = input('id');

        if(!$id || $id == 'undefined') return rejson(302, '必填字短缺失');

        $info = Db2::get_data('xy_cengji',[['id' => $id]]);

        $yaofangInfo = Db2::get_data('yaofang',[['id' => $info['yfid']]]);

        return rejson(200,'操作成功',['info' => $yaofangInfo]);
    }

    /**
     * @Notes: 诊断记录列表
     * @Interface getDiagnosticList
     * @author: Shaco
     * @Time: 2022/10/11-17:18
     */
    public function getDiagnosticList(){
        $user = $this->user;
        $list = Db2::get_datalist('zhenduanjilu',[['userid' => $user['id']]]);
        foreach ($list as $key => $val){
            if($val['text1']){
                $list[$key]['text1'] = json_decode($val['text1'],true);
            }
        }
        foreach ($list as $key1 => $val1){
            if($val1['text1']['classesList']){
                $list[$key1]['price'] = Db2::get_data('yaofang',[['id' => $val1['text1']['classesList'][0]['yaofang']['id']]])['price'];
            }
        }

        return rejson(200, '操作成功', $list);
    }

}